package com.AndroidApp.codememo;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.AndroidApp.codememo.databse.CodeMemoDbAdapter;
import com.AndroidApp.codememo.model.Codememo;

public class ListDetails extends Activity {

    private EditText mTitleText;
    private EditText mUnameText;
    private EditText mPasswordText;
    private EditText mOthrdetailsText;
    private Long mRowId;
    private CodeMemoDbAdapter mDbHelper;
    private Spinner mCategory;
    private Codememo mCodememo = null;

    @Override
    protected void onCreate(Bundle bundle) {

        super.onCreate(bundle);
        mDbHelper = new CodeMemoDbAdapter(this);
        mDbHelper.open();
        setContentView(R.layout.memo_edit);
        mCategory = (Spinner) findViewById(R.id.category);
        mTitleText = (EditText) findViewById(R.id.memo_edit_title);
        mUnameText = (EditText) findViewById(R.id.memo_edit_uname);
        mPasswordText = (EditText) findViewById(R.id.memo_edit_pswd);
        mOthrdetailsText = (EditText) findViewById(R.id.memo_edit_othrdetails);

        Button confirmButton = (Button) findViewById(R.id.memo_edit_button);
        mRowId = null;
        Bundle extras = getIntent().getExtras();
        mRowId = (bundle == null) ? null : (Long) bundle.getSerializable(CodeMemoDbAdapter.KEY_ROWID);
        if (extras != null) {
            mRowId = extras.getLong(CodeMemoDbAdapter.KEY_ROWID);
        }
        populateFields();
        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                setResult(RESULT_OK);
                finish();
            }

        });
    }

    private void populateFields() {
    	String unm="",psd="",othr="";
    	String key = "Naresh";

        if (mRowId != null) {
            Cursor Codememo = mDbHelper.fetchCodememo(mRowId);
            startManagingCursor(Codememo);
            String category = Codememo.getString(Codememo.getColumnIndexOrThrow(CodeMemoDbAdapter.KEY_CATEGORY));

            for (int i = 0; i < mCategory.getCount(); i++) {

                String s = (String) mCategory.getItemAtPosition(i);
                Log.e(null, s + " " + category);
                if (s.equalsIgnoreCase(category)) {
                    mCategory.setSelection(i);
                }
            }
            
            unm = xor(Codememo.getString(Codememo.getColumnIndexOrThrow(CodeMemoDbAdapter.KEY_UNAME)),key);
        	psd = xor(Codememo.getString(Codememo.getColumnIndexOrThrow(CodeMemoDbAdapter.KEY_PASSWORD)),key);
        	othr = xor(Codememo.getString(Codememo.getColumnIndexOrThrow(CodeMemoDbAdapter.KEY_OTHRDETAILS)),key);
        	
        	
        	mTitleText.setText(Codememo.getString(Codememo.getColumnIndexOrThrow(CodeMemoDbAdapter.KEY_TITLE)));
            mUnameText.setText(unm);
            mPasswordText.setText(psd);
            mOthrdetailsText.setText(othr);
           
            /*
            mTitleText.setText(Codememo.getString(Codememo.getColumnIndexOrThrow(CodeMemoDbAdapter.KEY_TITLE)));
            mUnameText.setText(Codememo.getString(Codememo.getColumnIndexOrThrow(CodeMemoDbAdapter.KEY_UNAME)));
            mPasswordText.setText(Codememo.getString(Codememo.getColumnIndexOrThrow(CodeMemoDbAdapter.KEY_PASSWORD)));
            mOthrdetailsText.setText(Codememo.getString(Codememo.getColumnIndexOrThrow(CodeMemoDbAdapter.KEY_OTHRDETAILS)));
            */
        }
        
    }

    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
        saveState();
        outState.putSerializable(CodeMemoDbAdapter.KEY_ROWID, mRowId);
    }

    @Override
    protected void onPause() {

        super.onPause();
        saveState();
    }

    @Override
    protected void onResume() {

        super.onResume();
        populateFields();
    }

    private void saveState() {

        mCodememo = new Codememo();
        
        //encategory="", entitle="",----cat="", tle="",
        String  unm="",psd="",othr="";
        String  enuname="",enpassword="",enothrs="";
        String key = "Naresh";
    	
    	//cat=(String) mCategory.getSelectedItem();
    	//tle = mTitleText.getText().toString();
    	unm = mUnameText.getText().toString();
    	psd = mPasswordText.getText().toString();
    	othr = mOthrdetailsText.getText().toString();
    	
	    
    	//encategory = cipher_additive_encrypt(cat.toLowerCase(),key);
    	//entitle = cipher_additive_encrypt(tle.toLowerCase(),key);
    	enuname = xor(unm,key);
    	enpassword = xor(psd,key);
    	enothrs = xor(othr,key);

   /*     
        mCodememo.category = (String) mCategory.getSelectedItem();
        mCodememo.title = mTitleText.getText().toString();
        mCodememo.uname_accno = mUnameText.getText().toString();
        mCodememo.password = mPasswordText.getText().toString();
        mCodememo.othrdetails = mOthrdetailsText.getText().toString();
        */
    	
    	mCodememo.category = (String) mCategory.getSelectedItem();
        mCodememo.title = mTitleText.getText().toString();
    	//mCodememo.category = encategory;
        //mCodememo.title = entitle;
        mCodememo.uname_accno = enuname;
        mCodememo.password = enpassword;
        mCodememo.othrdetails = enothrs;
    	
        if (mRowId == null) {
            long id = mDbHelper.createMemo(mCodememo);
            if (id > 0) {
                mRowId = id;
            }
        } else {
            mDbHelper.updateCodememo(mRowId, mCodememo);
        }
    }
    static String xor(String text,String key)
	{
    	String enc_dec = "";
		for(int i=0, j=0; i<text.length(); i++, j++)
		{
			if(j == key.length())
				j = 0;
			enc_dec += (char)(text.charAt(i) ^ key.charAt(j));
		}
		return enc_dec;
	}
}